package net.m2technologies.open_arm.samples.pojo;

import net.m2technologies.open_arm.OpenArmConfiguration;
import net.m2technologies.open_arm.transport.transaction.logging.LoggingMediatorConfiguration;
import net.m2technologies.open_arm.transport.transaction.messaging.MessagingMediatorConfiguration;

import javax.naming.Context;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
public class SimpleJmsApplication extends AbstractPojoApplication {

    protected SimpleJmsApplication(final String configurationFile) {
        super(configurationFile);
    }

    protected SimpleJmsApplication(final OpenArmConfiguration config) {
        super(config);
    }


    public static void main(final String[] args) {
        startActiveMQMessagingBroker();
        // We can do this with a file name, here passed as a command line parameter...
        //final SimpleApplication self = new SimpleApplication(args[0]);
        //
        // ... or, with a programmatic configuration.
        final SimpleJmsApplication self = new SimpleJmsApplication(obtainOpenArmConfiguration());
        self.doSomething();
        self.doSomethingWithSetupOverhead();
        self.doSomethingWithExceptionHandling();
        System.exit(0);
    }

    private static void startActiveMQMessagingBroker() {
        new Thread() {
            public void run() {
                org.activemq.broker.impl.Main.main(new String[]{"tcp://127.0.0.1:61616"});
            }
        }.start();
    }

    private static OpenArmConfiguration obtainOpenArmConfiguration() {
        return new OpenArmConfiguration() {
            public Map getMediatorConfigurations() {
                final Map result = new HashMap();
                result.put("net.m2technologies.open_arm.transport.transaction.logging.LoggingMediator",
                           new LoggingMediatorConfiguration());
                final Properties jndiInitialContextProperties = new Properties();
                jndiInitialContextProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                                                         "org.activemq.jndi.ActiveMQInitialContextFactory");
                jndiInitialContextProperties.setProperty(Context.PROVIDER_URL, "tcp://127.0.0.1:61616");
                result.put("net.m2technologies.open_arm.transport.transaction.messaging.MessagingMediator",
                           new MessagingMediatorConfiguration("QueueConnectionFactory",
                                                              2000,
                                                              5000,
                                                              true,
                                                              "myQ",
                                                              jndiInitialContextProperties));
                return result;
            }
        };
    }
}
